<?php
/**
 * @package		简易CMS
 * @subpackage	缓存函数
 * @version		$Id: function_cache.php 23 2012-06-11 18:04:03Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

if(!defined('IN_JYCMS')) {
	exit('Access Denied');
}

function updatecache($cachename = '') {
	$updatelist = empty($cachename) ? array() : (is_array($cachename) ? $cachename : array($cachename));
	if(!$updatelist) {
		@include_once libfile('cache/setting');
		build_cache_setting();
		$cachedir = ROOT_PATH.'./source/cache';
		$cachedirhandle = dir($cachedir);
		while($entry = $cachedirhandle->read()) {
			if(!in_array($entry, array('.', '..')) && preg_match("/^cache\_([\_\w]+)\.php$/", $entry, $entryr) && $entryr[1] != 'setting' && substr($entry, -4) == '.php' && is_file($cachedir.'/'.$entry)) {
				@include_once libfile('cache/'.$entryr[1]);
				if(function_exists('build_cache_'.$entryr[1]))
				call_user_func('build_cache_'.$entryr[1]);
			}
		}
	} else {
		foreach((array)$updatelist as $entry) {
			@include_once libfile('cache/'.$entry, '', false);
			if(function_exists('build_cache_'.$entry))
			call_user_func('build_cache_'.$entry);
		}
	}
}

function writetocache($script, $cachedata, $prefix = 'cache_') {
	global $_G;
	$dir = ROOT_PATH.'./data/cache/';
	cmsFS::autocreatePath($dir, 0777);
	$file = $dir.$prefix.$script.'.php';
	$buffer = "<?php\n//System cache file, DO NOT modify me!\n//Identify: ".md5($prefix.$script.'.php'.$cachedata.$_G['config']['security']['authkey'])."\n\n$cachedata?>";
	if(!cmsFS::write($file, $buffer)){
		exit('Can not write to cache files, please check directory ./data/ and ./data/cache/ .');
	}
}

function getcachevars($data, $type = 'VAR') {
	$evaluate = '';
	foreach((array)$data as $key => $val) {
		if(!preg_match("/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/", $key)) {
			continue;
		}
		if(is_array($val)) {
			$evaluate .= "\$$key = ".arrayeval($val).";\n";
		} else {
			$val = addcslashes($val, '\'\\');
			$evaluate .= $type == 'VAR' ? "\$$key = '$val';\n" : "define('".strtoupper($key)."', '$val');\n";
		}
	}
	return $evaluate;
}

function arrayeval($array, $level = 0) {
	if(!is_array($array)) {
		return "'".$array."'";
	}
	if(is_array($array) && function_exists('var_export')) {
		return var_export($array, true);
	}

	$space = '';
	for($i = 0; $i <= $level; $i++) {
		$space .= "\t";
	}
	$evaluate = "Array\n$space(\n";
	$comma = $space;
	if(is_array($array)) {
		foreach((array)$array as $key => $val) {
			$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
			$val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\''.addcslashes($val, '\'\\').'\'' : $val;
			if(is_array($val)) {
				$evaluate .= "$comma$key => ".arrayeval($val, $level + 1);
			} else {
				$evaluate .= "$comma$key => $val";
			}
			$comma = ",\n$space";
		}
	}
	$evaluate .= "\n$space)";
	return $evaluate;
}


function save_syscache($cachename, $data) {
	if (is_array ( $data )) {
		$ctype = 1;
	} else {
		$ctype = 0;
	}
	$cachefile = ROOT_PATH . './data/cache/cache_' . $cachename . '.php';
	@unlink ( $cachefile );
	$cachedata = '$data[\'' . $cachename . '\'] = ' . var_export ( $data, true ) . ";\n\n";
	$cachedata = "<?php\n//System cache file, DO NOT modify me!\n//Identify: " . md5 ( $cachename . $cachedata . $_G['config']['security']['authkey'] ) . "\n\n$cachedata?>";
	if (!cmsFS::write($cachefile, $cachedata)) {
		system_error ( 'Failure to store cache files, please check directory permissions!' );
	}
}
